/**
 * Copyright (C) 2009-2012, KingHool.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses.
 */
package com.kinghool.smodel;


import com.kinghool.ebos.commons.exception.ErrorCodeRuntimeException;
import com.kinghool.ebos.commons.utils.Assert;
import com.kinghool.ebos.commons.vo.PropertyHolder;
import com.kinghool.smodel.def.ModelDefinition;

/**
 * This class provides a skeletal implementation of the <tt>IModel</tt>
 * interface to minimize the effort required to implement this interface.
 * 
 * @Author Allen Deng(dengtailin@gmail.com) 
 */
public class Model extends PropertyHolder implements IModel {
	protected ModelDefinition modelDefinition;
	protected String modelId;
	
	public Model(ModelDefinition modelDefinition) {
		if (modelDefinition == null) {
			throw new IllegalArgumentException("Illegal argument: model definition could not be null");
		}
		this.modelDefinition = modelDefinition;
		this.modelId = modelDefinition.getId();
	}
	
	public Model(String modelId) {
		this(ModelDefinition.createDefaultModelDefinition(modelId));
	}
 
	public String getModelId() {
		return modelId;
	}
	
	public void setModelId(String modelId) {
		this.modelId = modelId;
	} 
	 
	public Object getProperty(String propertyName, Object... defaultValue) {
		checkProperty(propertyName);

		Object propVal = null;
		propertyName = formatName(propertyName);
		if (!dataCache.containsKey(propertyName)) {
			if (! Assert.isNullOrEmpty(defaultValue) && modelDefinition.getDefaultValue(propertyName) == null) {
				propVal = defaultValue[0];
			} else {
				propVal = modelDefinition.getDefaultValue(propertyName);
			}
		} else {
			propVal = dataCache.get(propertyName);
		}
		return propVal;
	}
 
	public boolean isProperty(String propertyName){ 
		return modelDefinition.isProperty(propertyName);
	}
	
	protected void checkProperty(String propertyName) {
		if (! isProperty(propertyName)) {
			throw new ErrorCodeRuntimeException(ErrorCodes.SMODEL_001.cloneWithArgs(propertyName));
		}
	} 
	
	public boolean isValid() { 
		return modelDefinition.validate(this).isValid();
	}

	public ValidationResult validate() { 
		return modelDefinition.validate(this);
	}

	public ValidationResult validate(ModelDefinition modelDefinition) {
		if (modelDefinition != null) {
			return modelDefinition.validate(this);
		}
		return ValidationResult.OK;
	}

	public boolean isUnset(String propertyName) {
		return !dataCache.containsKey(propertyName);
	}
	
	public Object getDefaultValue(String propertyName) {
		return modelDefinition.getDefaultValue(propertyName);
	}

	public ModelDefinition getModelDefinition() {
		return modelDefinition;
	} 
	
	protected boolean isCaseSensitive() {
		return modelDefinition.isCaseSensitive();
	}
	
	public String toString() {
		return getModelId();
	}
  
	public IModel clone() throws CloneNotSupportedException {
		Model model = (Model) super.clone();
	
		model.modelDefinition = (ModelDefinition) modelDefinition.clone();
		
		return model;
	}
}
